<?php
/**
 * 
 * @author chenqi
 * @uses 首页
 * @date 2015-07-10
 *
 */
class Home extends WX_Controller{
   public function __construct()
	{
		parent::__construct();
	}
       
	//首页
	public function index()
	{   $this->data['site_title']='用户中心';
            $this->load->model('user_model');
            $this->user=$this->user_model->checkUser(array('user.id'=>$this->user['id']));
            $this->data['user']=$this->user;
            $this->load->view('/wap/user',$this->data);
	}
        /**
         * 用户详情
         */
        public function user_detail(){
            
            $this->load->model('area_model');
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('user.id'=>$this->user['id']));
            $areas = unserialize($user['areas']);
             
		$area_array = explode(',', $areas['area_no']);
                
		if(count($area_array)!=3){
		    $area_array[0]='110000';
		    $area_array[1]='111000';
		    $area_array[2]='110101';
		}
		$province_area = $area_array[0];
		$city_area = $area_array[1];
		$area_where['parentno'] = 0;
		$area_list = $this->area_model->getArealist($area_where);
		$city_list = $this->area_model->getArealist(array('parentno'=>$province_area,'arealevel'=>2));
		$county_list = $this->area_model->getArealist(array('parentno'=>$city_area,'arealevel'=>3));
		$this->data['area_list'] = $area_list;
		$this->data['city_list'] = $city_list;
		$this->data['county_list'] = $county_list;
		$this->data['area_array'] = $area_array;
                $this->data['site_title']='用户详情';
            
            
            $parent_username='无';
            if($user['parent_id']){
                $this->load->model('user_model');
                $parent_user=$this->user_model->checkUser(array('user.id'=>$user['parent_id']));
                if(isset($parent_user['username'])){
                    $parent_username=$parent_user['username'];
                }
            }
            $user['parent_username']=$parent_username;
			$params=array();
			$params[]=$user['id'];
			$childs=$this->getchildcount($params);
			$user['all_next_count']=count($childs);
            $this->data['user']=$user;
            $this->load->view('/wap/user_detail',$this->data);
        }
        /**
         * 修改用户信息
         */
        public function user_edit() {
            if($_POST['password']){
			$_POST['password']=md5($_POST['password']);
			}else{
				unset($_POST['password']);
			}
			if($_POST['password2']){
			$_POST['password2']=md5($_POST['password2']);
			}else{
				unset($_POST['password2']);
			}
                foreach ($_POST as $key => $value) {
                    if(empty($value)){
                        unset($_POST[$key]);
                    }
                }
                $_POST['id']=  $this->user['id'];
		$data = $_POST;
		$data = $this->get_post($data,'areas');
		$data['update_time'] = time();
		$where['id'] = $data['id'];
                $this->load->model('user_model');
		$edit_result = $this->user_model->editUser($where,$data);
                echo json_encode(array('success'=>true));
        }
        public function add_user(){
            $this->data['site_title']='推荐会员';
            $area_array=array();
            $this->load->model('area_model');
            if(count($area_array)!=3){
                $area_array[0]='110000';
                $area_array[1]='111000';
                $area_array[2]='110101';
            }
            $province_area = $area_array[0];
            $city_area = $area_array[1];
            $area_where['parentno'] = 0;
            $area_list = $this->area_model->getArealist($area_where);
            $city_list = $this->area_model->getArealist(array('parentno'=>$province_area,'arealevel'=>2));
            $county_list = $this->area_model->getArealist(array('parentno'=>$city_area,'arealevel'=>3));
            $this->data['area_list'] = $area_list;
            $this->data['city_list'] = $city_list;
            $this->data['county_list'] = $county_list;
            $this->data['area_array'] = $area_array;
            
            $this->load->model('user_model');
            $this->user=$this->user_model->checkUser(array('user.id'=>$this->user['id']));
            
            $this->data['parent_user']=$this->user;
            $this->load->model('system_config_model');
            $system_config=$this->system_config_model->checkSystem_config(array('id'=>'1'));
            $this->data['system_config']=$system_config;
            $this->load->view('/wap/recommend_vip',$this->data);
        }
        /**
         * 查看用户下级
         */
        public function next_users(){
            $where=array();
            $parent_id=$this->user['id'];
            if(isset($_GET['parent_id'])){
                $parent_id=$_GET['parent_id'];
            }
            $where['user.parent_id']=$parent_id;
            $this->load->model('user_model');
            $user_list=$this->user_model->getUserAll($where);
            $user_tmp=array();
            foreach($user_list as $key => $value) {
                // $user_list[$key]['group_performance']=$group_performance['group_performance'];
                 if($value['parent_id'] ){
                     if(in_array($value['parent_id'], $user_tmp)){
                         $user_list[$key]['parent_name']=$user_tmp[$value['parent_id']]['parent_name'];
                         $user_list[$key]['ptrue_name']=$user_tmp[$value['parent_id']]['true_name'];
                         $user_list[$key]['pcreate_time']=$user_tmp[$value['parent_id']]['pcreate_time'];
                     }else{
                         $user = $this->user_model->checkUser(array('user.id'=>$value['parent_id']));
                         $user_tmp[$value['parent_id']]['parent_name']=$user['username'];
                         $user_tmp[$value['parent_id']]['ptrue_name']=$user['true_name'];
                         $user_tmp[$value['parent_id']]['pcreate_time']=$user['create_time'];
                         $user_list[$key]['pcreate_time']=$user_tmp[$value['parent_id']]['pcreate_time'];
                         $user_list[$key]['parent_name']=$user_tmp[$value['parent_id']]['parent_name'];
                         $user_list[$key]['ptrue_name']=$user_tmp[$value['parent_id']]['ptrue_name'];
                     }
                 }else{
                     $user_list[$key]['parent_name']='无';
                     $user_list[$key]['parent_nick']='无';
                }
            }
          
            $this->data['users']=$user_list;
            $this->load->view('/wap/my_vip',$this->data);
        }
        /**
         * 检测用户名是否重复
         */
        public function check_name(){
            $this->load->model('user_model');
            $success=true;
            $where=array();
            $where['username']=$_POST['username'];
            $user=$this->user_model->checkUser($where);
            if($user ){
                $success=false;
            }
            echo json_encode($success);
        }
        /**
         * 添加用户
         */
        public function add(){
            $is_success=false;
            $msg='';
            $this->load->model('user_model');
            $user_tmp=$this->user_model->checkUser(array('username'=>$_POST['username']));
            if(empty($user_tmp)){
            $_POST['parent_id']=$this->user['id'];
            $_POST['password']=md5($_POST['password']);
            $_POST['password2']=md5($_POST['password2']);
            $_POST['s_time']=  time();
            $data=$_POST;
            $data['create_time']=time();
            $data = $this->get_post($data,'areas');
            $this->load->model('user_level_model');
            $user_level=$this->user_level_model->checkUserLevel(array('is_default'=>'1'));
            $data['user_level']=$user_level['id'];
            
           
             //查找当前用户数据
            $this->load->model('user_model');
            $this->user=$this->user_model->checkUser(array('user.id'=>$this->user['id']));
            //查找后台设置的报单价格
            $this->load->model('system_config_model');
            $system_config=$this->system_config_model->checkSystem_config(array('id'=>'1'));
            if( $this->user['system_balance'] < $system_config['price']){
                $is_success=false;
                $msg='报单币不足';
            }else{
            
                $add_result=$this->user_model->addUser($data);
                
                
                $user=array();
                $parent_user=array();
                $parent_parent_user=array();
                if($add_result){
                    //扣掉报单币
                    $system_balance=$this->user['system_balance']-$system_config['price'];
                    $this->user_model->editUser(array('id'=>$this->user['id']),array('system_balance'=>$system_balance));
                    //记录日志
                    $money_logdata[]=array(
                            'user_id'=>$this->user['id'],
                            'username'=>$this->user['username'],
                            'true_name'=>$this->user['true_name'],
                            'money'=>$system_config['price'],
                            'type' =>'adduser',
                            'remark'=>'您添加了下级'.$data['true_name'].'，扣减'.$system_config['price'].'报单币',
                            'create_time'=>  time()
                        );
                        $this->add_money_log($money_logdata);
                    
                    
                    $user=$this->user_model->checkUser(array('id'=>$add_result));
                    if($_POST['parent_id']){

                        $parent_user=$this->user_model->checkUser(array('id'=>$_POST['parent_id']));
                        $this->user_model->editUser(array('id'=>$user['id']),array('parent_path'=>$user['id'].','.$parent_user['parent_path']));
                        $zhitu= $this->zhitui2($user,$parent_user);
                        if($zhitu){

                        }
                    }
                    if(isset($parent_user['parent_id'])){
                        $parent_parent_user=$this->user_model->checkUser(array('id'=>$parent_user['parent_id']));
                        $this->load->model('user_level_model');
                        $user_level=$this->user_level_model->checkUserLevel(array('id'=>$parent_user['user_level']));
                        $distribution=$this->shouru($user, $parent_user, $parent_parent_user,$user_level['use_id']);
                        if($distribution){

                        }
                        
                    }
                                   
                    $this->getperformance($user);
                    //$this->update_level();
                    $this->updatelevel($user);

                }
                $is_success=true;
            
            }
        }else{
            $is_success=false;
            $msg='用户已经存在';
        }//exit;
           echo json_encode(array('success'=>$is_success,'msg'=>$msg));
        }
		public function tuichu(){
			 $this->load->library('wx_visitor');
			 $this->wx_visitor->signout();
			echo json_encode(array('success'=>true));
		}
		//获取用户的下级总数
		 public function getchildcount(&$newids=array(),$next=0){
			
			 if(!isset($this->user_model)){
				 $this->load->model('user_model');
			 }
			$ids=array();
			$ids=$this->user_model->getFeildList(array('parent_id'=>$newids[$next]),'id');
			
			if(count($ids) >0){
				foreach($ids as $key=>$value){
					$newids[]=$value['id'];
				}
				
			}
			$next++;
			if(isset($newids[$next])){
				$this->getchildcount($newids,$next);
			}
			return $newids;
		 }
}